using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;

namespace Geoconcepts.Common
{
    /// <summary>
    /// Class used for benchmarking and auditing of web services use.
    /// </summary>
    public class Audit
    {
        /// <summary>
        /// Adds a Log of each configured web service execution
        /// </summary>
        /// <param name="strConnectionString"></param>
        /// <param name="strCallingMethod"></param>
        /// <param name="strMethod"></param>
        /// <param name="strParameters"></param>
        /// <param name="dMiliseconds"></param>
        /// <param name="strTest"></param>
        /// <param name="IP"></param>
        public static void GrabarLog(string strConnectionString, string strCallingMethod, string strMethod, string strParameters, double dMiliseconds, string strTest,string IP)
        {
            SqlConnection sqlConnect = null;
            try
            {
                sqlConnect = new SqlConnection(strConnectionString);
                sqlConnect.Open();

                SqlCommand sqlCommand = new SqlCommand("spgcAuditLogsIns",sqlConnect);
                sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                sqlCommand.Parameters.Add(new SqlParameter("@CallingMethod", strCallingMethod));
                sqlCommand.Parameters.Add(new SqlParameter("@CalledMethod", strMethod));
                sqlCommand.Parameters.Add(new SqlParameter("@Parameters", strParameters));
                sqlCommand.Parameters.Add(new SqlParameter("@ElapsedTime", dMiliseconds));
                sqlCommand.Parameters.Add(new SqlParameter("@Test", strTest));
                sqlCommand.Parameters.Add(new SqlParameter("@IP", strCallingMethod));
                sqlCommand.Parameters.Add(new SqlParameter("@Country", ""));
                sqlCommand.Parameters.Add(new SqlParameter("@City", ""));
                sqlCommand.Parameters.Add(new SqlParameter("@Date", DateTime.Now));
                
                int numFilas = sqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                 if (sqlConnect != null)
                 {
                     sqlConnect.Close();
                 }
             }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strConnectionString"></param>
        /// <returns></returns>
        public static string TestConnection(string strConnectionString)
        {
            string strResult = "";
            try
            {
                SqlConnection sqlConnect = new SqlConnection(strConnectionString);
                sqlConnect.Open();
                sqlConnect.Close();
                return "Success";
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            
        }
    }
}
